var QQMapWX = require('../libs/qqmap-wx-jssdk.js');
var _ajax = require('./httpUtil.js');

var _key = "AFBBZ-26436-QYCSL-ESUEC-SGGWJ-KEBAS";
var qqmapsdk;
// 实例化API核心类
qqmapsdk = new QQMapWX({
	key: _key
});

function reverseGeocoder(latitude, longitude,_fun){
	// 调用接口
	qqmapsdk.reverseGeocoder({
		location: {
			latitude: latitude,
			longitude: longitude
		},
		success: function (res) {
			console.log(res);
				return typeof _fun == "function" && _fun(res.result)
			
		},
		fail: function (res) {
			console.log(res);
		},
		complete: function (res) {
			console.log(res);
		}
	});
}

function getLocation(_fun){
	wx.getLocation({
		type: 'gcj02',
		success: (res) => {
			wx.setStorageSync('location', { "lat": res.latitude, 'lng': res.longitude });
			return typeof _fun == "function" && _fun(res)
		}
	})
}

function getAddressList(lat,lng,_fun){
	// 事件触发，调用接口
		var _this = this;

	var mm = {};
	mm.boundary = 'nearby(' + lat + ',' + lng + ',1000)';
	mm.page_index = '1';
	mm.page_size = 20;
	mm.output = 'json';
	mm.key = _key;
	_ajax.reqGetFor('https://apis.map.qq.com/ws/place/v1/search', mm, function (res) {

		console.log(res);
		var mks = []
		for (var i = 0; i < res.data.length; i++) {
			mks.push({ // 获取返回结果，放到mks数组中
				title: res.data[i].title,
				addr:res.data[i].address,
				id: res.data[i].id,
				latitude: res.data[i].location.lat,
				longitude: res.data[i].location.lng,
				iconPath: "/resources/my_marker.png", //图标路径
				width: 20,
				height: 20
			})
		}
		//_this.setData({ //设置markers属性，将搜索结果显示在地图中
		return typeof _fun == "function" && _fun(mks)
	})


return;
		// 调用接口
		qqmapsdk.search({
			keyword: '小区',  //搜索关键词
			location: [lat, lng].join(","),  //设置周边搜索中心点
			success: function (res) { //搜索成功后的回调
				var mks = []
				for (var i = 0; i < res.data.length; i++) {
					mks.push({ // 获取返回结果，放到mks数组中
						title: res.data[i].title,
						id: res.data[i].id,
						latitude: res.data[i].location.lat,
						longitude: res.data[i].location.lng,
						iconPath: "/resources/my_marker.png", //图标路径
						width: 20,
						height: 20
					})
				}
				//_this.setData({ //设置markers属性，将搜索结果显示在地图中
					return typeof _fun == "function" && _fun(mks)
				//})
			},
			fail: function (res) {
				console.log(res);
			},
			complete: function (res) {
				console.log(res);
			}
		});
}
function getDistance (lat1, lng1, lat2, lng2) {
	lat1 = lat1 || 0;
	lng1 = lng1 || 0;
	lat2 = lat2 || 0;
	lng2 = lng2 || 0;

	var rad1 = lat1 * Math.PI / 180.0;
	var rad2 = lat2 * Math.PI / 180.0;
	var a = rad1 - rad2;
	var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;

	var r = 6378137;
	var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));

    /*if (distance > 1000){
      distance = Math.round(distance / 1000);
    }*/

	return distance;
}


module.exports = {
	reverseGeocoder: reverseGeocoder,
	getLocation: getLocation,
	getAddressList: getAddressList,
	getDistance: getDistance
}